package com.tzq.autoticket.utils;

import com.tzq.autoticket.Config;
import com.tzq.autoticket.bean.Tourist;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.http.util.TextUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class XlsUtils {
    public static List<Tourist> readTourist(String xlsPath) {
        List<Tourist> list = new ArrayList<>();
        Workbook workbook = null;
        try {
            workbook = Workbook.getWorkbook(new File(xlsPath));
            Sheet sheet = workbook.getSheet(0);
            for (int i = 1; i < sheet.getRows(); i++) {
                Tourist tourist = new Tourist();
                Cell nameCell = sheet.getCell(0, i);
                Cell cardTypeCell = sheet.getCell(1, i);
                Cell cardNoCell = sheet.getCell(2, i);
                Cell phoneCell = sheet.getCell(3, i);
                if (nameCell == null || cardTypeCell == null || cardNoCell == null || phoneCell == null) {
                    Log.err("xls cell is invaid  for row " + i);
                } else if (TextUtils.isEmpty(nameCell.getContents())
                        || TextUtils.isEmpty(cardTypeCell.getContents())
                        || TextUtils.isEmpty(cardNoCell.getContents())
                        || TextUtils.isEmpty(phoneCell.getContents())) {
                    Log.err("xls cell content is empty for row " + i);
                } else {
                    tourist.setName(nameCell.getContents().trim());
                    tourist.setCardType(cardTypeCell.getContents().trim());
                    tourist.setCardNo(cardNoCell.getContents().trim());
                    tourist.setPhoneNumber(phoneCell.getContents().trim());
                    list.add(tourist);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                workbook.close();
            }
        }
        return list;
    }

    public static void writeTourist(List<Tourist> list, String path) {
        File tmp = new File(Config.getInstance().getTmpXlsPath());
        File file = new File(path);

        WritableWorkbook workbook = null;
        WritableSheet sheet = null;
        try {
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            workbook = Workbook.createWorkbook(file, Workbook.getWorkbook(tmp));
            sheet = workbook.getSheet(0);
//            final String[] titles = {"姓名", "证件类型", "证件号", "电话"};
//            for (int i = 0; i < titles.length; i++) {
//                Label label = new Label(i, 0, titles[i]);
//                sheet.addCell(label);
//            }
            int row = 1;
            for (Tourist tourist : list) {
                Log.info(tourist);
                Label nameLabel = new Label(0, row, tourist.getName());
//                nameLabel.setCellFeatures(sheet.getWritableCell(0, row).getWritableCellFeatures());
//                nameLabel.setCellFormat(sheet.getWritableCell(0, row).getCellFormat());
                sheet.addCell(nameLabel);


                Label cardTypeLabel = new Label(1, row, tourist.getCardType());
//                cardTypeLabel.setCellFeatures(sheet.getWritableCell(1, row).getWritableCellFeatures());
//                cardTypeLabel.setCellFormat(sheet.getWritableCell(1, row).getCellFormat());
                sheet.addCell(cardTypeLabel);

                Label cardNoLabel = new Label(2, row, tourist.getCardNo());
//                cardNoLabel.setCellFeatures(sheet.getWritableCell(2, row).getWritableCellFeatures());
//                cardNoLabel.setCellFormat(sheet.getWritableCell(2, row).getCellFormat());
                sheet.addCell(cardNoLabel);

                Label phoneLabel = new Label(3, row, tourist.getPhoneNumber());
//                phoneLabel.setCellFeatures(sheet.getWritableCell(3, row).getWritableCellFeatures());
//                phoneLabel.setCellFormat(sheet.getWritableCell(3, row).getCellFormat());
                sheet.addCell(phoneLabel);
                row++;
            }
            workbook.write();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
//        List<Tourist> list = getTestList();
//        writeTourist(list, "D:\\work\\test.xls");
//        readTourist("/Users/zqtang/Downloads/touristinfo.xls");
//        Calendar c=Calendar.getInstance();
//        c.setTime(new Date());
//        Log.info(c.get(Calendar.MONTH)+"  "+c.get(Calendar.DAY_OF_MONTH));
    }
}
